package com.jsy.seckill.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.util.TokenDecode;
import com.jsy.common.vo.Result;
import com.jsy.seckill.pojo.SeckillOrder;
import com.jsy.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:jsy
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/seckill/order")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;

    /***
     * SeckillOrder分页条件搜索实现
     * @param seckillOrder: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillOrder>> findPage(@RequestBody(required = false) SeckillOrder seckillOrder, @PathVariable int page, @PathVariable int size) {
        //调用SeckillOrderService实现分页条件查询SeckillOrder
        PageInfo<SeckillOrder> pageInfo = seckillOrderService.findPage(seckillOrder, page, size);
        return new Result<PageInfo<SeckillOrder>>("查询成功！", pageInfo);
    }

    /***
     * SeckillOrder分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<SeckillOrder>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用SeckillOrderService实现分页查询SeckillOrder
        PageInfo<SeckillOrder> pageInfo = seckillOrderService.findPage(page, size);
        return new Result<PageInfo<SeckillOrder>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param seckillOrder 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search")
    public Result<List<SeckillOrder>> findList(@RequestBody(required = false) SeckillOrder seckillOrder) {
        //调用SeckillOrderService实现条件查询SeckillOrder
        List<SeckillOrder> list = seckillOrderService.findList(seckillOrder);
        return new Result<List<SeckillOrder>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用SeckillOrderService实现根据主键删除
        seckillOrderService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改SeckillOrder数据
     * @param seckillOrder 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody SeckillOrder seckillOrder) {
        //调用SeckillOrderService实现修改SeckillOrder
        seckillOrderService.update(seckillOrder);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增SeckillOrder数据
     * @param seckillOrder 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody SeckillOrder seckillOrder) {
        //调用SeckillOrderService实现添加SeckillOrder
        seckillOrderService.add(seckillOrder);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询SeckillOrder数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<SeckillOrder> findById(@PathVariable Long id) {
        //调用SeckillOrderService实现根据主键查询SeckillOrder
        SeckillOrder seckillOrder = seckillOrderService.findById(id);
        return new Result<SeckillOrder>("查询成功！", seckillOrder);
    }

    /***
     * 查询SeckillOrder全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<SeckillOrder>> findAll() {
        //调用SeckillOrderService实现查询所有SeckillOrder
        List<SeckillOrder> list = seckillOrderService.findAll();
        return new Result<List<SeckillOrder>>("查询成功！", list);
    }


    /***
     *秒杀下单
     * @return 查询结果
     */
    @GetMapping("/create")
    public Result orderCreate(String time, Long id) {
        try {
            //查看redis中秒杀商品的所有数据
            String username = TokenDecode.getUserInfo().get("username");
            Long outTradeNo  = seckillOrderService.orderCreate(username, time, id);
            if (outTradeNo!=null){
                return new Result("秒杀商品成功！", outTradeNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,e.getMessage());
        }
        return new Result(false, StatusCode.ERROR,"秒杀商品失败！");
    }
}
